package com.william.killian.trivia;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.william.killian.trivia.model.Answer;
import com.william.killian.trivia.model.Question;
import com.william.killian.trivia.util.Util;

public class QuizEditor extends ListActivity implements OnItemClickListener, OnItemLongClickListener
{
    private ListView mList;
    private ArrayAdapter<Question> mAdapter;
    
    @Override
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);
        setTheme (android.R.style.Theme_Light);
        int index = getIntent ().getIntExtra ("index", 0) - 1;
        Util.current = index;
        mAdapter = new ArrayAdapter<Question> (this, R.layout.list_item, Util.currentQuiz ().getQuestions ());
        mList = getListView ();
        mList.addHeaderView (new TextView (this) {{
            setGravity (Gravity.CENTER_HORIZONTAL);
            setText (Util.currentQuiz().getName ());
            setTextSize (24);
        }});
        mList.addFooterView (new Button (this) {{
            setGravity (Gravity.CENTER_HORIZONTAL);
            setText (R.string.text_question_add);
            setOnClickListener (new OnClickListener()
            {
                public void onClick (View arg0)
                {
                    ArrayList<Answer> a = new ArrayList <Answer> ();
                    a.add (new Answer ("Answer 1"));
                    a.add (new Answer ("Answer 2"));
                    Question q = new Question ();
                    q.setText ("New Question");
                    q.setAnswers (a);
                    Util.currentQuiz ().getQuestions ().add (q);
                    mAdapter.notifyDataSetChanged ();
                }
            });
        }});
        mList.setAdapter (mAdapter);
        mList.setOnItemClickListener (this);
        mList.setOnItemLongClickListener (this);
    }

    public boolean onItemLongClick (AdapterView <?> av, View v, final int pos, long id)
    {
        if (pos != 0)
        {
            new AlertDialog.Builder (this)
                .setTitle (R.string.title_question_delete)
                .setMessage (R.string.text_question_delete)
                .setCancelable (false)
                .setPositiveButton (R.string.yes, new DialogInterface.OnClickListener()
                {
                    public void onClick (DialogInterface dialog, int which)
                    {
                        Util.currentQuiz ().deleteQuestion (pos);
                        mAdapter.notifyDataSetChanged ();
                    }
                })
                .setNegativeButton (R.string.no, null)
                .show ();
        }
        return true;
    }

    public void onItemClick (AdapterView <?> av, View v, int pos, long id)
    {
        if (pos != 0)
        {
            Intent intent = new Intent (this, QuestionEditor.class);
            intent.putExtra ("index", pos);
            startActivity (intent);
        }
    }
    
    public void onResume ()
    {
        super.onResume ();
        if (mAdapter != null)
        {
            mAdapter.notifyDataSetChanged ();
        }
    }
    
    public void onPause ()
    {
        super.onPause ();
        Util.currentQuiz().save ();
    }
    
    public void onDestroy ()
    {
        super.onDestroy ();
        Util.currentQuiz().save ();
    }
    
}
